/**
 * Perform the element default action.
 *
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"6BCA0BBF-E92B-45BD-B28B-6A1F05E14B08"}
 * @AllowToRunInFind
 */
function buscar(event) 
{
	if (globals.marca_primer_find == 0)
	{
		globals.marca_primer_find = 1
		var estado_find=controller.find()
		if (estado_find==false)
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay movimientos de cuenta corriente','OK')
		   	return;
		}
		proveedor_id = globals.gProveedorDesde;
		
		var found = controller.search()
		if (found == 0)
		{
			globals.DIALOGS.showWarningDialog('Aviso','No hay movimientos de ctacte para ese proveedor','OK')
		 	var $win = application.getWindow('imput_ctacte');
			$win.hide()
			$win.destroy()
		}
	}
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"CCB9B61E-F00E-45F2-B757-05E31A9887CE"}
 */
function inicio(firstShow, event) 
{
	globals.gtotal_creditos=0;
	globals.gtotal_debitos=0;
	globals.gtotal_comprob=0;
	buscar();
	totales();

}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"68787BA9-D861-4B67-99FB-9CCE23253AD7"}
 */
function totales(event) 
{
	globals.gtotal_creditos = 0;
	globals.gtotal_debitos = 0;
	globals.gtotal_comprob = 0;
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total; i++)
	{
		var $rcd = foundset.getRecord(i)
		if ($rcd.marcado_ctacte == 1)
		{
			if($rcd.ctacte_pr_to_tab_mov_pr.d_h == 2)
				//globals.gtotal_debitos+=saldo_signo; // 21/12/2010
				globals.gtotal_debitos += ($rcd.importe - $rcd.saldo);
			else
				//globals.gtotal_creditos+=saldo_signo;  // 21/12/2010
				globals.gtotal_creditos += ($rcd.importe - $rcd.saldo);
			//globals.gtotal_comprob=globals.gtotal_debitos + globals.gtotal_creditos; // 21/12/2010
		}
	}
	globals.gtotal_comprob = globals.gtotal_debitos - globals.gtotal_creditos;
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"44FDF820-DDBF-43B5-8D3A-848F38E69748"}
 */
function onDataChange_marcado_ctacte(oldValue, newValue, event) 
{
	totales();    /// cuando hay muchos reg. hacer la pasada por de todos los reg por pantalla
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"70F489E9-3DAD-447A-9CD0-BAD1B7DA88D8"}
 */
function marcar_todo(event) 
{
	globals.gtotal_creditos=0;
	globals.gtotal_debitos=0;
	globals.gtotal_comprob=0;
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total; i++)
	{
		var $rcd = foundset.getRecord(i)
		$rcd.marcado_ctacte = 1;
		if($rcd.ctacte_pr_to_tab_mov_pr.d_h == 2)
			//globals.gtotal_debitos+=saldo_signo;  // 21/12/2010
			globals.gtotal_debitos += ($rcd.importe - $rcd.saldo);
		else
			// globals.gtotal_creditos+=saldo_signo;  // 21/12/2010
			globals.gtotal_creditos += ($rcd.importe - $rcd.saldo);
				
	}
	// globals.gtotal_comprob=globals.gtotal_debitos + globals.gtotal_creditos; //21/12/2010
	globals.gtotal_comprob=globals.gtotal_debitos - globals.gtotal_creditos;
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"1DAACD8F-31C7-415A-835D-AC53FF89C029"}
 */
function desmarcar_todo(event) 
{	
	var $total = databaseManager.getFoundSetCount(foundset)
	for (var i = 1 ; i <= $total; i++)
	{
		var $rcd = foundset.getRecord(i)
		if ($rcd.marcado_ctacte==1)
			$rcd.marcado_ctacte=0;
	}
	globals.gtotal_creditos=0;
	globals.gtotal_debitos=0;
	globals.gtotal_comprob=0;
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A7F956C4-981D-4592-BCB1-A6446DD33710"}
 */
function confirmar(event) 
{
	//crear los reg. de imput detalle
	
	//borrar  lo que tenga en imput_detalle 
	forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.deleteAllRecords()
	
	//recorre los marcados = 1
	var found = controller.getMaxRecordIndex()	
	if (found > 0)
	{
		globals.gtotal_comprob=0 
		var aux_saldo=0 // 02/07/2010 para que considere signo del movto 
		for (var i = 1 ; i <= found; i++)
		{		 
			controller.setSelectedIndex(i)
			if (marcado_ctacte==1)
			{
				//grabar lo que marcado en imput_detalle
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.newRecord()
				  
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.cod_mov=cod_mov
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.fecha=fecha_mov
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.importe=globals.redondeo(importe-saldo)
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.imp_original=globals.redondeo(importe-saldo)
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.nro_comprob=nro_factura
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.letra=letra
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.sucursal=sucursal
				if (concepto==null)
					concepto=" ";
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.concepto=concepto
				forms.imput_pr_inv_1.imput_cabecera_to_imput_detalle.ctacte_pr_id=ctacte_pr_id
				if (ctacte_pr_to_tab_mov_pr.d_h == 1)
				{
				 	aux_saldo = globals.redondeo((importe-saldo) * (-1))
				}
				else
				{
				  	aux_saldo = globals.redondeo(importe-saldo) 
				} 
				globals.gtotal_comprob+=aux_saldo
			}
		}
	}
	var $win = application.getWindow('imput_ctacte');
	$win.hide()
	$win.destroy()
}
